package com.fitness.fitness.kafka;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fitness.fitness.modal.course.entity.Course;
import com.fitness.fitness.modal.datavisual.entity.*;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.ICourseService;
import com.fitness.fitness.service.IDataVisualService;
import com.fitness.fitness.ws.Message;
import com.fitness.fitness.ws.WebSocketServer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class ADSMsgResolver {

    private final IDataVisualService dataVisualService;
    private final ICourseService courseService;

    private void getRateVisuals() throws Exception {
        List<RateVisual> rateVisuals = dataVisualService.getRateVisuals();
        Message<List<RateVisual>> message = new Message<>();
        message.setType("rateVisual");
        message.setContent(rateVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
    }

    private ServerResponseEntity<List<PriceVisual>> getPriceVisuals() throws Exception {
        List<PriceVisual> priceVisuals = dataVisualService.getPriceVisuals();
        Message<List<PriceVisual>> message = new Message<>();
        message.setType("priceVisual");
        message.setContent(priceVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(priceVisuals);
    }

    private void getCategoryVisuals() throws Exception {
        List<CategoryVisual> categoryVisuals = dataVisualService.getCategoryVisuals();
        Message<List<CategoryVisual>> message = new Message<>();
        message.setType("categoryVisual");
        message.setContent(categoryVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
    }

    private void updateGenderVisuals(List<String> list) throws Exception {
//        List<>
        List<GenderVisual> genderVisuals = new ArrayList<>();
        for(String data : list){
            JSONArray jsonArray =JSONUtil.parseArray(data);
            GenderVisual genderVisual = new GenderVisual();
            genderVisual.setGender(jsonArray.get(0).toString().equals("F") ? "女性" : "男性");
            genderVisual.setNum(Integer.parseInt(jsonArray.get(1).toString()));
            genderVisuals.add(genderVisual);
        }
//        genderVisuals = dataVisualService.getGenderVisuals();
        dataVisualService.saveOrUpdateGenderVisualBatch(genderVisuals);
        Message<List<GenderVisual>> message = new Message<>();
        message.setType("genderVisual");
        message.setContent(genderVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
    }

    private void getAgeVisuals() throws Exception {
        List<AgeVisual> ageVisuals = dataVisualService.getAgeVisuals();
        Message<List<AgeVisual>> message = new Message<>();
        message.setType("ageVisual");
        message.setContent(ageVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
    }

    private void updateCourseTopNVisuals(List<String> list) throws IOException {
        List<CourseTopNVisual> topNVisuals = new ArrayList<>();
        Map<Long, Course> courseMap = courseService.getCourseMap();
        for(String data : list){
            JSONArray jsonArray =JSONUtil.parseArray(data);
            CourseTopNVisual topNVisual = new CourseTopNVisual();
            Long courseId = Long.valueOf(jsonArray.get(0).toString());
            topNVisual.setCourseId(courseId);
            topNVisual.setCourseName(courseMap.get(courseId).getName());
            topNVisual.setScore(((BigDecimal) jsonArray.get(jsonArray.size() - 1)).setScale(2, RoundingMode.DOWN).doubleValue());
            topNVisuals.add(topNVisual);
        }
        Message<List<CourseTopNVisual>> message = new Message<>();
        message.setType("courseTopNVisual");
        message.setContent(topNVisuals);
        dataVisualService.updateCourseTopNVisuals(topNVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
    }

    public void resolve(String message) {
        Map<String,Object> map = JSONUtil.parseObj(message);
        if(map.containsKey("adsGender")){
            try {
                updateGenderVisuals((List)map.get("adsGender"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(map.containsKey("adsAge")){
            try {
                getAgeVisuals();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(map.containsKey("adsCategory")){
            try {
                getCategoryVisuals();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(map.containsKey("adsPrice")){
            try {
                getPriceVisuals();
            } catch(Exception e) {}
        }else if(map.containsKey("adsRate")){
            try {
                getRateVisuals();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if (map.containsKey("adsCourseTopN")){
            try {
                updateCourseTopNVisuals((List)map.get("adsCourseTopN"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        String s = "{\"adsGender\":[\"[F,8]\",\"[M,19]\"]}";
//        Map<String,Object> map = JSONUtil.parseObj(s);
//        System.out.println(JSONUtil.parseArray(((List)map.get("adsGender")).get(0)).get(1) instanceof Integer);
//        System.out.println(map);
//    }
}
